package com.dalamao.example.netty;

import android.util.Log;

import java.io.UnsupportedEncodingException;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * Created by MENRED-WHB on 2016/10/28.
 */

public class NettyClientHandler extends ChannelInboundHandlerAdapter {


    public static final String TAG = "NettyClientHandler";





    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) { // (2)
        ByteBuf buf = (ByteBuf) msg;
        byte[] req = new byte[buf.readableBytes()];
        buf.readBytes(req);


        try {
            String body = new String(yiHuo(req), "utf-8");

            Log.i(TAG, "channelRead: " + body);


        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }


    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        // Close the connection when an exception is raised.
        cause.printStackTrace();
        ctx.close();
    }


    public byte[] yiHuo(byte[] k) {
        byte[] tem = new byte[k.length];
        for (int i = 0; i < k.length; i++) {
            tem[i] = (byte) (k[i] ^ 0xa1);
        }
        return tem;
    }


}
